<?php

/*
 * Created on May 15, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class Service_model extends CI_Model {

    /**
     * @var unknown_type
     */
    var $id;
    /**
     * @var unknown_type
     */
    var $category_parent_id;
    /**
     * @var unknown_type
     */
    var $name_vi;
    /**
     * @var unknown_type
     */
    var $name_en;
    /**
     * @var unknown_type
     */
    var $description_vi;
    /**
     * @var unknown_type
     */
    var $description_en;
    /**
     * @var unknown_type
     */
    var $picture;
    /**
     * @var unknown_type
     */
    var $active;
    /*
     * @access	public
     */

    function __construct() {
        parent::__construct();
    }

    function service_model() {
        parent::Model();
    }

    function create($category) {
        $sql = "INSERT INTO `gtb_service` " .
                "( `friendly_url`, " .
                " `vi_name`, `en_name`, `vi_description`, " .
                "`en_description`,  " .
                " `active`, `image`, `date_add`, `date_upd`) " .
                "VALUES ";

        $sql .= "(" .
                $this->db->escape($category['friendly_url']) . ", " .
                $this->db->escape($category['vi_name']) . ", " .
                $this->db->escape($category['en_name']) . ", " .              
                "'" . ($category['vi_description']) . "', " .
                "'" . ($category['en_description']) . "', " .
                intval($category['active']) . ", " .
                $this->db->escape($category['image']) . ", " .
                $this->db->escape($category['date_add']) . ", " .
                $this->db->escape($category['date_upd']) . "" .
                ")";


        $this->db->query($sql);
        //return $this->db->affected_rows();
        return $this->db->insert_id();
    }

    function update($category) {
        $sql = "UPDATE gtb_service
				SET
				friendly_url = " . $this->db->escape($category['friendly_url']) . ", " .
                "vi_name = " . $this->db->escape($category['vi_name']) . "," .
                "en_name = " . $this->db->escape($category['en_name']) . ", " .
                "vi_description = '" . ($category['vi_description']) . "', " .
                "en_description = '" . ($category['en_description']) . "', " .
                "active = " . intval($category['active']) . ", " .
                "image = " . $this->db->escape($category['image']) . ", " .
                "date_upd = " . $this->db->escape($category['date_upd']) .
                " WHERE id_service = " . intval($category['id_service']);
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    function delete($id) {
        if (!is_array($id))
            $id = array($id);
        $ids = implode(',', $id);
        $sql = "DELETE FROM gtb_service WHERE id_service IN ($ids)";
        //$sql = "DELETE FROM gtb_pcategory WHERE id_pcategory = " . intval($id);
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    function getAll($offset, $number_record, $active = -1, $order_by = array('position' => 'ASC')) {
        $order_with = array();
        foreach ($order_by as $column => $direction) {
            $order_with[] = 'i.' . $column . ' ' . $direction;
        }
        $order_with = implode(',', $order_with);


        $result = null;
        $sql = "SELECT vi_name, vi_name as cname, id_service as item_id, active " .
                " FROM gtb_service i " .
                " WHERE  i.active = " . intval($active) . " OR -1 = " . intval($active) .
                " ORDER BY $order_with" .
                " LIMIT $offset, $number_record";

        $query = $this->db->query($sql);
        $result = $query->result();

        return $result;
    }

    function countAll($category = -1, $active = -1) {
        $result = null;
        $sql = "SELECT COUNT(*) as num_record" .
                " FROM gtb_service " .
                " WHERE  active = " . intval($active) . " OR -1 = " . intval($active);

        $query = $this->db->query($sql);
        $result = $query->result();

        return $result[0]->num_record;
    }

    function getAllByIds($ids) {
        $ids = implode(',', $ids);
        $result = null;
        $sql = "SELECT n.* " .
                " FROM gtb_news n " .
                " WHERE id_news IN ($ids) " .
                " ORDER BY n.id_news DESC";

        $query = $this->db->query($sql);
        $result = $query->result();

        return $result;
    }

    function getMaxPosition($category_parent_id) {

        $sql = "SELECT MAX(position) as position_max
				FROM category
				WHERE category_parent_id = $category_parent_id";
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0]->position_max;
        }

        return 0;
    }

    function getById($id) {

        $sql = "SELECT *
				FROM gtb_service
				WHERE id_service = " . intval($id);
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result[0];
        }

        return null;
    }

    function getByFriendlyUrl($link_rewrite) {

        $sql = "SELECT *
				FROM gtb_news
				WHERE friendly_url = " . $this->db->escape($link_rewrite);
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0];
        }

        return null;
    }

    function getNewest() {
        $sql = "SELECT *
		FROM gtb_service
                WHERE active = 1
                ORDER BY id_service
                LIMIT 0,1 ";
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result[0];
        }

        return null;
    }

    //function getUserBy
//
}

// EOF

